package net.zoneland.geological.devicedata.controller;

import org.apache.log4j.Logger;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.zoneland.geological.devicedata.entity.Tbl_data_bak_log;
import net.zoneland.geological.devicedata.service.Tbl_data_bak_logServiceI;

import net.zoneland.greenleaf.common.pagination.Pagination;
import net.zoneland.greenleaf.system.controller.BaseController;

/**
 * 数据备份日志操作控制层(Controller|Action)

 * 表   名：TBL_DATA_BAK_LOGCONTROLLER<br/>
 * 注   释：数据备份日志<br/>
 * 作   者：李义<br/>
 * 日   期：2017-10-17 13:32:40
**/
@Controller
@RequestMapping("tbl_data_bak_log")
public class Tbl_data_bak_logController extends BaseController { 

	/**
	 * 如果需要系统对此Controller的访问进行监控，记录服务访问的时间，耗时，那么请将monitor属性设置为“true”
	 */

	public boolean monitor = false; 

	private static Logger logger = Logger.getLogger(Tbl_data_bak_logController.class);

	//获取类的名称
	@SuppressWarnings("unused")
	private String clazzName = new Object() {
		public String getClassName() {
			String clazzName = this.getClass().getName();
			return clazzName.substring(0, clazzName.lastIndexOf('$'));
		}
	}.getClassName();

	/**
	 * tbl_data_bak_logService
	 * @param tbl_data_bak_logService
	**/
	@Autowired
	private Tbl_data_bak_logServiceI tbl_data_bak_logService;

	/**
	 * 根据ID获取信息数据
	 * @param id
	 * @return
	**/
	@RequestMapping( method = RequestMethod.GET, value = "/{id}", produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public Object get( @PathVariable("id") String id ){
		Map<String, Object> map = new HashMap<String, Object>();
		Tbl_data_bak_log entity = null;
		Boolean check = true;

		map.put("id", id);
		if( id == null || "(0)".equals( id ) || id.isEmpty() ){
			check = false;
			map.put("result", "error");
			map.put("message", "系统获取到的ID不正常，ID:" + id );
		}
		if( check ){
			try {
				entity = tbl_data_bak_logService.get( map );
			} catch (Exception e) {
				check = false;
				map.put("result", "error");
				map.put("message", "系统根据ID获取对象失败!");
				logger.error("系统根据ID获取对象发生异常。ID:" + id, e);
			}
		}
		if( check ){
			if( entity != null ){
				map.put( "result", "success");
				map.put( "data", entity );
			}else{
				map.put( "result", "error");
				map.put( "message", "数据不存在！ID:" + id );
			}
		}
		return map;
	}

	/**
	 * 获取数据备份日志列表
	 * 返回JSON格式数据，如果不需要分页，您可以考虑将pagesize设置为足够大的数字，系统默认为20
	 * @param Map<String, Object> paramMap
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/list" )
	@ResponseBody
	public Object list( @RequestBody Map<String, Object> paramMap ) {
		String page = "1";
		Integer pagesize = 20, totalRecord=0;
		List<Tbl_data_bak_log> list  = null;
		Map<String, Object> map=new HashMap<String,Object>();
		Pagination<?> pagination = null;
		Boolean check = true;

		if( paramMap == null ){
			check = false;
			map.put( "result", "error");
			map.put( "message", "参数列表为空，无法进行数据查询!");
		}
		if( check ){
			if( paramMap.get( "page" )!=null){
				page = paramMap.get( "page" ).toString();
			}
			if( paramMap.get( "pagesize") != null){
				try{
					pagesize = Integer.parseInt(paramMap.get( "pagesize" ).toString());
				}catch( Exception e ){
					pagesize = 20;
				}
			}
		}
		if( check ){
			try{
				totalRecord = tbl_data_bak_logService.getListCount( paramMap );
			}catch( Exception e ){
				check = false;
				map.put( "result", "error");
				map.put( "message", "系统根据条件查询信息数据条目总数时发生异常!");
				logger.error( e );
			}
		}
		if( check ){
			try{
				pagination = Pagination.newPagination( pagesize, page, totalRecord );
				paramMap.put( "startRow", pagination.getStartRow() );
				paramMap.put( "pageSize", pagesize );
				/**
			 	* 如果需要按某列进行排序，可以再多给两个参数：orderColName, orderType
			 	* paramMap.put( "orderColName", "ID");
			 	* paramMap.put( "orderType", "DESC");
			 	**/
			}catch( Exception e ){
				check = false;
				map.put( "result", "error" );
				map.put( "message", "系统根据条件进行分页信息计算时发生异常！" );
				logger.error( e );
			}
		}
		if( check ){	
			if( totalRecord>0 ){
				try{
					list = tbl_data_bak_logService.getList( paramMap );
				}catch( Exception e ){
					check = false;
					map.put( "result", "error" );
					map.put( "message", "系统根据条件进行信息列表查询信息数据时发生异常!" );
					logger.error( e );
				}
			}
		}
		if( check ){
			if( list == null ){
				list = new ArrayList<Tbl_data_bak_log>();
			}
		}
		if( check ){
			map.put( "result", "success" );
			map.put( "message", "系统查询信息数据成功!" );
			map.put( "pagination", pagination );
			map.put( "data", list );
		}
		return map;
}

	/**
	 * 增加或编辑一个数据备份日志信息
	 * 返回JSON格式数据
	 * @param entity
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/save" )
	@ResponseBody
	public Object save( @RequestBody Map<String, Object> paramMap ){
		Map<String, Object> map = new HashMap<String, Object>();
		Tbl_data_bak_log entity = null;
		Boolean check = true;

		if( paramMap == null ){
			check = false;
			map.put( "result", "error");
			map.put( "message", "需要保存的信息数据为空!");
		} else {
			try {
				entity = (Tbl_data_bak_log)mapToObject( paramMap, Tbl_data_bak_log.class );
			} catch (Exception e) {
				check = false;
				map.put( "result", "error");
				map.put( "message", "将数据转换为对象时发生异常!");
				logger.error( "将数据转换为对象时发生异常!", e );
			}
		}
		if( check ){
			map = validate( entity, map );
			if( map.get( "result" ) == null || "error".equalsIgnoreCase( map.get( "result" ).toString() ) ){
				check = false;
			}
		}
		if( check ){
			try{
				entity = tbl_data_bak_logService.addOrUpdate( entity );
				map.put( "result", "success");
				map.put( "message", "保存数据备份日志信息数据成功!");
				map.put( "data", entity );
			}catch( Exception e ){
				map.put( "result", "error" );
				map.put( "message", "保存数据备份日志信息数据失败!" );
				logger.error("保存数据备份日志信息数据失败!", e);
			}
		}
		return map;
	}

	/**
	 * 对需要进行保存的对象里的信息校验
	 * @param entity
	 * @param map 
	 * @return
	 */
	private Map<String, Object> validate( Tbl_data_bak_log entity, Map<String, Object> map ) {
		//TODO 这里进行所有的数据校验工作
		map.put("result", "success");
		return map;
	}

	/**
	 * 根据ID删除一个数据备份日志信息
	 * 返回JSON格式数据
	 * @param int id
	**/
	@RequestMapping( method = RequestMethod.DELETE, value = "/{id}" )
	@ResponseBody
	public Object deleteById( @PathVariable( "id" ) String id ){
		Map<String, Object> map = new HashMap<String, Object>();
		Tbl_data_bak_log entity = null;
		Boolean check = true;

		map.put( "id", id );
		if( id == null || id.isEmpty() || "id".equalsIgnoreCase( id )){
			check = false;
			map.put( "result", "error" );
			map.put( "message", "需要删除的信息ID为空!" );
		}
		if( check ){
			try {
				entity = tbl_data_bak_logService.get( map );
				if( entity == null ){
					check = false;
					map.put( "result", "error" );
					map.put( "message", "需要删除的信息数据不存在失败!ID:" + id );
				}
			} catch (Exception e) {
				check = false;
				map.put( "result", "error" );
				map.put( "message", "根据ID查询需要删除的信息数据失败!ID:" + id );
				logger.error( "根据ID查询需要删除的信息数据失败!ID:" + id, e);
			}
		}
		if( check ){
			try {
				tbl_data_bak_logService.delete( map );
				map.put( "result", "success" );
				map.put( "message", "成功删除一个信息数据!" );
			} catch (Exception e) {
				check = false;				map.put( "result", "error");
				map.put( "message", "删除信息数据失败!ID:" + id );
				logger.error( "删除信息数据失败!ID:" + id, e);
			}
		}
		return map;
	}

	/**
	 * 根据ID删除多个数据备份日志信息
	 * 返回JSON格式数据
	 * @param Map<String, Object> paramMap
	**/
	@RequestMapping( method = RequestMethod.POST, value = "/delete/batch" )
	@ResponseBody
	public Object deleteByIds( @RequestBody Map<String, Object> paramMap ){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids_str = null;
		Boolean check = true;

		if( paramMap == null || paramMap.get( "ids" ) == null ){
			check = false;
			map.put( "result", "error" );
			map.put( "message", "参数列表为空，无法进行数据删除!" );
		}
		if( check ){
			ids_str = (String)paramMap.get( "ids" );
			if( ids_str != null && ids_str.isEmpty() ){
				check = false;
				map.put( "result", "error" );
				map.put( "message", "参数列表中ID列表为空，无法进行数据删除!" );
			}else{
				map.put( "ids", ids_str.split( "," ) );
			}
		}
		if( check ){
			try {
				tbl_data_bak_logService.delete( map );
				map.put( "result", "success" );
				map.put( "message", "成功删除信息数据!" );
			} catch (Exception e) {
				map.put( "result", "error" );
				map.put( "message", "删除信息数据失败!ID:" + paramMap.get( "ids" ) );
				logger.error( "系统信息数据失败!ID:" + paramMap.get( "ids" ), e );
			}
		}
		return map;
	}
}
